/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.statistic;

import java.io.File;
import java.io.FileWriter;
import org.maze.Population.Population;

/**
 *
 * @author Thomas
 */
public class Statistic {
    private File file;
    private FileWriter writer;
    private double previousOnline=0;
    private double previousOffline=0;

    public Statistic(String filePath) throws Exception{
        file = new File(filePath);
        file.createNewFile();
        writer = new FileWriter(file);
        writer.write("generation,online-perf,offline-perf,best,average,diff\n");
        writer.flush();
    }

    public void addLine(Population pop) throws Exception{

        String generation = pop.getGeneration()+",";
        String online =calculateOnlinePerformance(pop.getAverageScore(),  pop.getGeneration())+",";
        String offline=calculateOfflinePerformance(pop.getFitest().getFitnessScore(),  pop.getGeneration())+",";
        String best=pop.getFitest().getFitnessScore()+",";
        String average=pop.getAverageScore()+",";
        String diff=pop.getAverageDiversity()+"\n";
        String line = generation+online+offline+best+average+diff;
        writer.write(line);
        writer.flush();
    }

    public double calculateOnlinePerformance(double value, int generation){
        double res=0;
        res = ((generation-1)*previousOnline+value)/generation;
        previousOnline=res;
        return res;
    }

    public double calculateOfflinePerformance(double value, int generation){
        double res=0;
        res = ((generation-1)*previousOffline+value)/generation;
        previousOffline=res;
        return res;
    }
}
